<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>活动抽奖</title>
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');
        
        :root {
            --bg: #2b2d42;
            --accent1: #ef233c;
            --accent2: #8d99ae;
            --accent3: #edf2f4;
            --pixel-size: 4px;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Press Start 2P', cursive;
            image-rendering: pixelated;
        }

        body {
            min-height: 100vh;
            background: var(--bg);
            color: var(--accent3);
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
            overflow-x: hidden;
        }

        .container {
            max-width: 800px;
            width: 100%;
            text-align: center;
        }

        h1 {
            font-size: 2.5rem;
            margin-bottom: 1.5rem;
            color: var(--accent1);
            text-shadow: 4px 4px 0 #000;
            position: relative;
            display: inline-block;
        }

        h1::after {
            content: '';
            position: absolute;
            bottom: -10px;
            left: 0;
            width: 100%;
            height: 4px;
            background: var(--accent1);
            box-shadow: 2px 2px 0 #000;
        }

        .pixel-border {
            border: 4px solid var(--accent3);
            box-shadow: 8px 8px 0 #000;
            position: relative;
            background: #414669;
        }

        .pixel-panel {
            padding: 20px;
            margin: 0 auto 30px;
            max-width: 500px;
        }

        .grid-container {
            display: grid;
            grid-template-columns: repeat(3,1fr);
            gap: 12px;
            margin: 25px auto;
        }

        .grid-item {
            aspect-ratio: 1/1;
            background: #4a5276;
            border: 4px solid var(--accent3);
            box-shadow: inset 4px 4px 0 #6d79a8, inset -4px -4px 0 #2f3249;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            transition: all 0.2s;
            position: relative;
        }

        .grid-item.active {
            background: #ef233c;
            box-shadow: inset 4px 4px 0 #ff6b7a, inset -4px -4px 0 #8d0010;
            transform: translate(2px, 2px);
        }

        .grid-item.win {
            animation: pixelPulse 0.5s infinite alternate;
        }

        @keyframes pixelPulse {
            from {
                background: #ef233c;
                box-shadow: inset 4px 4px 0 #ff6b7a, inset -4px -4px 0 #8d0010;
            }
            to {
                background: #ff0a22;
                box-shadow: inset 4px 4px 0 #ff8d99, inset -4px -4px 0 #ef0015;
            }
        }

        .prize-icon {
            font-size: 2rem;
            margin-bottom: 8px;
            filter: drop-shadow(2px 2px 0 #000);
        }

        .prize-name {
            font-size: 0.6rem;
            color: var(--accent3);
            margin-bottom: 5px;
        }

        .prize-value {
            font-size: 0.5rem;
            color: var(--accent2);
        }

        .button-container {
            display: flex;
            justify-content: center;
            gap: 15px;
            flex-wrap: wrap;
            margin: 25px 0;
        }

        .pixel-btn {
            padding: 12px 24px;
            font-size: 0.8rem;
            border: none;
            cursor: pointer;
            transition: all 0.1s;
            position: relative;
            background: var(--accent3);
            color: #000;
            box-shadow: 0 6px 0 #8d99ae, 0 8px 0 #000;
        }

        .pixel-btn:active, .pixel-btn:disabled {
            transform: translateY(4px);
            box-shadow: 0 2px 0 #8d99ae, 0 4px 0 #000;
        }

        .pixel-btn:disabled {
            opacity: 0.7;
            cursor: not-allowed;
        }

        .btn-start {
            background: #4ef037;
            box-shadow: 0 6px 0 #3bc427, 0 8px 0 #000;
        }

        .btn-stop {
            background: #ef233c;
            box-shadow: 0 6px 0 #c21c30, 0 8px 0 #000;
        }

        .btn-reset {
            background: #3dc2ff;
            box-shadow: 0 6px 0 #2a9ace, 0 8px 0 #000;
        }

        .result-container {
            min-height: 150px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .result-title {
            color: var(--accent1);
            font-size: 1.2rem;
            margin-bottom: 15px;
            text-shadow: 2px 2px 0 #000;
        }

        .result-content {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
        }

        .result-icon {
            font-size: 2.5rem;
            margin-bottom: 10px;
            filter: drop-shadow(2px 2px 0 #000);
        }

        .result-text {
            font-size: 0.9rem;
            text-align: center;
        }

        .result-name {
            font-weight: bold;
            color: var(--accent1);
            margin-bottom: 5px;
        }

        .result-value {
            font-size: 0.8rem;
            color: var(--accent2);
        }

        .screen-glitch {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: var(--accent1);
            opacity: 0;
            z-index: 2;
            mix-blend-mode: overlay;
            pointer-events: none;
        }

        .history-list {
            display: flex;
            flex-direction: column;
            gap: 10px;
            max-height: 200px;
            overflow-y: auto;
            padding: 10px;
            background: #35394e;
            border: 4px solid var(--accent3);
            box-shadow: inset 4px 4px 0 #4a5276, inset -4px -4px 0 #232536;
        }

        .history-item {
            display: flex;
            align-items: center;
            padding: 10px;
            background: #414669;
            border: 2px solid var(--accent3);
        }

        .history-icon {
            font-size: 1.5rem;
            margin-right: 10px;
            filter: drop-shadow(2px 2px 0 #000);
        }

        .history-details {
            flex-grow: 1;
            text-align: left;
        }

        .history-name {
            font-size: 0.7rem;
            margin-bottom: 4px;
        }

        .history-time {
            font-size: 0.5rem;
            color: var(--accent2);
        }

        .slider-container {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-top: 15px;
        }

        input[type=range] {
            flex: 1;
            -webkit-appearance: none;
            height: 8px;
            border-radius: 0;
            background: #4a5276;
            outline: none;
            border: 2px solid var(--accent3);
            box-shadow: inset 2px 2px 0 #6d79a8, inset -2px -2px 0 #2f3249;
        }

        input[type=range]::-webkit-slider-thumb {
            -webkit-appearance: none;
            width: 20px;
            height: 20px;
            border-radius: 0;
            background: var(--accent1);
            cursor: pointer;
            border: 2px solid var(--accent3);
            box-shadow: 2px 2px 0 #000;
        }

        /* 像素化动画 */
        @keyframes pixelate {
            0% { clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%); }
            20% { clip-path: polygon(0 0, 83% 0, 100% 17%, 100% 100%, 17% 100%, 0 83%); }
            40% { clip-path: polygon(0 0, 67% 0, 100% 33%, 100% 100%, 33% 100%, 0 67%); }
            60% { clip-path: polygon(0 0, 50% 0, 100% 50%, 100% 100%, 50% 100%, 0 50%); }
            80% { clip-path: polygon(0 0, 33% 0, 100% 67%, 100% 100%, 67% 100%, 0 33%); }
            100% { clip-path: polygon(0 0, 0 0, 100% 100%, 100% 100%, 100% 100%, 0 100%); }
        }

        .pixel-animation {
            animation: pixelate 0.5s forwards;
        }

        /* 扫描线效果 */
        .scanlines {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(to bottom, 
                rgba(255,255,255,0.03) 50%, 
                rgba(0,0,0,0.1) 50%);
            background-size: 100% 4px;
            pointer-events: none;
            z-index: 999;
            opacity: 0.3;
        }

        /* 屏幕闪烁效果 */
        .screen-flash {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: white;
            opacity: 0;
            pointer-events: none;
            z-index: 1000;
        }

        /* 复古CRT显示效果 */
        .crt::after {
            content: " ";
            display: block;
            position: fixed;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            background: rgba(0, 0, 0, 0.1);
            opacity: 0.2;
            z-index: 998;
            pointer-events: none;
            animation: flicker 0.15s infinite;
        }

        @keyframes flicker {
            0% { opacity: 0.1; }
            5% { opacity: 0.1; }
            10% { opacity: 0.2; }
            15% { opacity: 0.1; }
            20% { opacity: 0.3; }
            25% { opacity: 0.1; }
            30% { opacity: 0.2; }
            35% { opacity: 0.1; }
            40% { opacity: 0.2; }
            45% { opacity: 0.1; }
            50% { opacity: 0.3; }
            55% { opacity: 0.1; }
            60% { opacity: 0.2; }
            65% { opacity: 0.1; }
            70% { opacity: 0.2; }
            75% { opacity: 0.1; }
            80% { opacity: 0.3; }
            85% { opacity: 0.1; }
            90% { opacity: 0.2; }
            95% { opacity: 0.1; }
            100% { opacity: 0.2; }
        }

        .confetti {
            position: fixed;
            width: 8px;
            height: 8px;
            top: -10px;
            border-radius: 50%;
            animation: fall 3s linear forwards;
            z-index: 1000;
        }

        @keyframes fall {
            to {
                transform: translateY(110vh) rotate(720deg);
                opacity: 0;
            }
        }
    </style>
</head>
<body class="crt">
    <div class="scanlines"></div>
    <div class="screen-flash" id="screenFlash"></div>
    
    <div class="container">
        <h1>活动抽奖</h1>

        <!-- 速度控制 -->
        <div class="pixel-panel pixel-border">
            <div class="screen-glitch" id="screenGlitch"></div>
            <h3>速度控制</h3>
            <div class="slider-container">
                <input type="range" id="speedSlider" min="1" max="10" value="5">
                <span id="speedValue">5</span>
            </div>
        </div>

        <!-- 九宫格 -->
        <div class="pixel-panel pixel-border">
            <div class="screen-glitch" id="screenGlitch2"></div>
            <div class="grid-container">
                <div class="grid-item"><div class="prize-icon">🎮</div><div class="prize-name">游戏主机</div><div class="prize-value">¥2999</div></div>
                <div class="grid-item"><div class="prize-icon">📱</div><div class="prize-name">智能手机</div><div class="prize-value">¥3999</div></div>
                <div class="grid-item"><div class="prize-icon">💻</div><div class="prize-name">笔记本电脑</div><div class="prize-value">¥5999</div></div>
                <div class="grid-item"><div class="prize-icon">🎧</div><div class="prize-name">无线耳机</div><div class="prize-value">¥899</div></div>
                <div class="grid-item"><div class="prize-icon">⌚</div><div class="prize-name">智能手表</div><div class="prize-value">¥1299</div></div>
                <div class="grid-item"><div class="prize-icon">🎁</div><div class="prize-name">神秘礼盒</div><div class="prize-value">¥1999</div></div>
                <div class="grid-item"><div class="prize-icon">🎫</div><div class="prize-name">优惠券</div><div class="prize-value">满1000减200</div></div>
                <div class="grid-item"><div class="prize-icon">🧧</div><div class="prize-name">现金红包</div><div class="prize-value">¥888</div></div>
                <div class="grid-item"><div class="prize-icon">🎨</div><div class="prize-name">画板套装</div><div class="prize-value">¥599</div></div>
            </div>
        </div>

        <!-- 按钮 -->
        <div class="button-container">
            <button class="pixel-btn btn-start" id="startButton">开始</button>
            <button class="pixel-btn btn-stop" id="stopButton" disabled>停止</button>
            <button class="pixel-btn btn-reset" id="resetButton">重置</button>
        </div>

        <!-- 结果 -->
        <div class="pixel-panel pixel-border result-container">
            <div class="screen-glitch" id="screenGlitch3"></div>
            <div class="result-title">抽奖结果</div>
            <div class="result-content" id="resultContent">
                <div class="result-icon">🎰</div>
                <div class="result-text">准备开始...</div>
            </div>
        </div>

        <!-- 抽奖记录 -->
        <div class="pixel-panel pixel-border">
            <div class="screen-glitch" id="screenGlitch4"></div>
            <h2>抽奖记录</h2>
            <div class="history-list" id="historyList">
                <div class="history-item">
                    <div class="history-icon">📝</div>
                    <div class="history-details">
                        <div class="history-name">暂无记录</div>
                        <div class="history-time">--:--:--</div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        /* === 基本数据 === */
        const prizes = [...document.querySelectorAll('.grid-item')].map(el => ({
            name: el.querySelector('.prize-name').textContent,
            value: el.querySelector('.prize-value').textContent,
            icon: el.querySelector('.prize-icon').textContent
        }));
        const gridItems = document.querySelectorAll('.grid-item');
        const startBtn = document.getElementById('startButton');
        const stopBtn = document.getElementById('stopButton');
        const resetBtn = document.getElementById('resetButton');
        const resultBox = document.getElementById('resultContent');
        const historyBox = document.getElementById('historyList');
        const speedSlider = document.getElementById('speedSlider');
        const speedValue = document.getElementById('speedValue');

        let isSpin = false, current = 0, win = -1, timer = null;
        let base = 50, factor = 5;
        
        speedSlider.addEventListener('input', e => { 
            factor = +e.target.value; 
            speedValue.textContent = factor; 
        });

        const speed = () => Math.max(10, base / factor);

        /* === 功能函数 === */
        function reset() {
            clearTimeout(timer); 
            isSpin = false; 
            current = 0; 
            win = -1;
            gridItems.forEach(el => el.classList.remove('active', 'win'));
            resultBox.innerHTML = '<div class="result-icon">🎰</div><div class="result-text">准备开始...</div>';
            startBtn.disabled = false; 
            stopBtn.disabled = true;
        }
        
        function start() {
            if (isSpin) return;
            reset(); 
            isSpin = true; 
            startBtn.disabled = true; 
            stopBtn.disabled = false;
            win = Math.floor(Math.random() * 9);
            resultBox.innerHTML = '<div class="result-icon">🎡</div><div class="result-text">抽奖中...</div>';
            
            // 添加特效
            triggerGlitch();
            run();
        }
        
        function run() {
            gridItems.forEach(el => el.classList.remove('active'));
            gridItems[current].classList.add('active');
            current = (current + 1) % 9;
            if (current === win && Math.random() > .7) { 
                setTimeout(end, 100); 
                return; 
            }
            timer = setTimeout(run, speed());
        }
        
        function stopLottery() {
            if (!isSpin) return;
            clearTimeout(timer); 
            isSpin = false;
            if (win < 0) win = Math.floor(Math.random() * 9);
            gridItems.forEach(el => el.classList.remove('active'));
            gridItems[win].classList.add('active', 'win');
            
            // 添加特效
            triggerFlash();
            showResult();
            startBtn.disabled = false; 
            stopBtn.disabled = true;
        }
        
        function end() { 
            stopLottery(); 
        }
        
        function showResult() {
            const p = prizes[win];
            resultBox.innerHTML = `
                <div class="result-icon">${p.icon}</div>
                <div class="result-text">
                    <div class="result-name">${p.name}</div>
                    <div class="result-value">${p.value}</div>
                </div>`;
            confetti(); 
            addHistory(p);
        }
        
        function confetti() {
            const colors = ['#ef233c', '#4ef037', '#3dc2ff', '#ffea00', '#edf2f4'];
            for (let i = 0; i < 100; i++) {
                const c = document.createElement('div');
                c.className = 'confetti';
                c.style.left = Math.random() * 100 + 'vw';
                c.style.background = colors[Math.floor(Math.random() * colors.length)];
                c.style.width = c.style.height = Math.random() * 8 + 4 + 'px';
                document.body.appendChild(c);
                setTimeout(() => c.remove(), 3000);
            }
        }
        
        let history = [];
        function addHistory(p) {
            const t = new Date();
            history.unshift({ ...p, time: t.toLocaleTimeString() });
            if (history.length > 5) history.pop();
            updateHistory();
        }
        
        function updateHistory() {
            if (!history.length) {
                historyBox.innerHTML = `
                    <div class="history-item">
                        <div class="history-icon">📝</div>
                        <div class="history-details">
                            <div class="history-name">暂无记录</div>
                            <div class="history-time">--:--:--</div>
                        </div>
                    </div>`;
                return;
            }
            
            historyBox.innerHTML = '';
            history.forEach(h => {
                const d = document.createElement('div');
                d.className = 'history-item';
                d.innerHTML = `
                    <div class="history-icon">${h.icon}</div>
                    <div class="history-details">
                        <div class="history-name">${h.name}</div>
                        <div class="history-time">${h.time}</div>
                    </div>`;
                historyBox.appendChild(d);
            });
        }

        // 添加复古游戏特效
        function triggerGlitch() {
            const glitches = document.querySelectorAll('.screen-glitch');
            glitches.forEach(glitch => {
                glitch.style.opacity = '0.7';
                setTimeout(() => {
                    glitch.style.opacity = '0';
                }, 100);
            });
        }
        
        function triggerFlash() {
            const flash = document.getElementById('screenFlash');
            flash.style.opacity = '0.8';
            setTimeout(() => {
                flash.style.opacity = '0';
            }, 50);
        }
        
        // 随机触发小故障效果
        setInterval(() => {
            if (Math.random() > 0.7) {
                triggerGlitch();
            }
        }, 3000);

        /* === 事件 === */
        startBtn.addEventListener('click', start);
        stopBtn.addEventListener('click', stopLottery);
        resetBtn.addEventListener('click', reset);
        
        // 初始化
        reset(); 
        updateHistory();
    </script>
</body>
</html>